//
//  RemotePlayerFileManager.m
//  Player
//
//  Created by Burn on 2019/7/4.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "RemotePlayerFileManager.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface RemotePlayerFileManager ()

@property(copy,nonatomic)NSString *cachePath;
@property(copy,nonatomic)NSString *tempPath;

@end

@implementation RemotePlayerFileManager

IMSingletonM

- (NSString *)cachePath{
    if (!_cachePath) {
        _cachePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:@"MENK_REMOTE_PLAYER_FILE"];
        if (![[NSFileManager defaultManager]fileExistsAtPath:_cachePath]) {
            [[NSFileManager defaultManager]createDirectoryAtPath:_cachePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    return _cachePath;
}


- (NSString *)tempPath{
    if (!_tempPath) {
        _tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MENK_REMOTE_PLAYER_TEMP_FILE"];
        if (![[NSFileManager defaultManager]fileExistsAtPath:_tempPath]) {
            [[NSFileManager defaultManager]createDirectoryAtPath:_tempPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    return _tempPath;
}


- (BOOL)internalCacheFileExists:(NSURL *)url{
    return [[NSFileManager defaultManager] fileExistsAtPath:[self cacheFilePathWith:url]];
}

- (BOOL)cacheFileExists:(NSURL *)url{
    return [[NSFileManager defaultManager]fileExistsAtPath:[self cacheFilePathWith:url]];
}

- (BOOL)tempFileExists:(NSURL *)url{
    return [[NSFileManager defaultManager]fileExistsAtPath: [self tempFilePathWith:url]];
}

- (NSString *)cacheFilePathWith:(NSURL *)url{
    return [self.cachePath stringByAppendingPathComponent:url.lastPathComponent];
}

- (NSString *)tempFilePathWith:(NSURL *)url{
    return [self.tempPath stringByAppendingPathComponent:url.lastPathComponent];
}

- (NSInteger)cacheFileSizeWith:(NSURL *)url{
    if (![self cacheFileExists:url]) {
        return 0;
    }
    NSString *path = [self cacheFilePathWith:url];
    NSError *error;
    NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
    if (!error) {
        return [dict[NSFileSize] integerValue];
    }else{
        return 0;
    }
}

- (NSInteger)tempFileSizeWith:(NSURL *)url{
    if (![self tempFileExists:url]) {
        return 0;
    }
    NSString *path = [self tempFilePathWith:url];
    NSError *error;
    NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
    if (!error) {
        return [dict[NSFileSize] integerValue];
    }else{
        return 0;
    }
}

- (NSString *)contentTypeWith:(NSURL *)url{
    NSString *path = [self cacheFilePathWith:url];
    CFStringRef fileExtension = (__bridge CFStringRef)[path pathExtension];
    CFStringRef contentTypeCF = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil);
    if (contentTypeCF) {
        return (__bridge NSString *)contentTypeCF;
    }else{
        return @"public.mp3";
    }
    
}

- (void)moveFileWith:(NSURL *)url{
    NSString *tempFilePath = [self tempFilePathWith:url];
    NSString *cacheFilePath = [self cacheFilePathWith:url];
    [[NSFileManager defaultManager]moveItemAtPath:tempFilePath toPath:cacheFilePath error:nil];
}

- (void)clearTempWith:(NSURL *)url{
    [[NSFileManager defaultManager]removeItemAtPath:[self tempFilePathWith:url] error:nil];
}

@end
